/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amuthu.expression.function.indicator;

import org.amuthu.DataStore;
import org.amuthu.expression.Expression;
import org.amuthu.expression.function.FunctionEvaluator;
import org.amuthu.expression.terminal.QuoteExprType;
import org.amuthu.indicator.IndicatorQuoteType;
import org.amuthu.parser.ExpressionVisitor;

/**
 *
 * @author prabhu
 */
public abstract class AbstractFunctionEvaluator implements FunctionEvaluator
{
   private int period;
   private int offset;

   public int getPeriod()
   {
      return period;
   }

   public void setPeriod(int period)
   {
      this.period = period;
   }

   public int getOffset()
   {
      return offset;
   }

   public void setOffset(int offset)
   {
      this.offset = offset;
   }

   public AbstractFunctionEvaluator()
   {
   }

   public IndicatorQuoteType getIndicatorQuoteType(QuoteExprType quoteType)
   {
      switch (quoteType)
      {
         case DAY_OPEN:
            return IndicatorQuoteType.OPEN;
         case DAY_CLOSE:
            return IndicatorQuoteType.CLOSE;
         case DAY_HIGH:
            return IndicatorQuoteType.HIGH;
         case DAY_LOW:
            return IndicatorQuoteType.LOW;
         case DAY_VOLUME:
            return IndicatorQuoteType.VOLUME;
         default:
            throw new AssertionError();
      }
   }

   abstract public boolean updateParameters(DataStore dataStore, Expression[] arguments, ExpressionVisitor visitor);

   public boolean updatePeriodAndOffset(DataStore dataStore, Expression[] arguments, ExpressionVisitor visitor)
   {
      if (arguments.length == 0)
      {
         return false;
      }

      int _period = (int) visitor.evaluate(arguments[0]);

      if (_period < 0)
      {
         return false;
      }

      int startOffset = (arguments.length > 1) ? (int) visitor.evaluate(arguments[1]) : 0;

      setPeriod(_period);
      setOffset(startOffset);

      return true;
   }

}
